623. 在二叉树中增加一行
为保证权益,题目请参考 623. 在二叉树中增加一行(From LeetCode).
解决方案1
Python
python
# 623. 在二叉树中增加一行
# https://leetcode.cn/problems/add-one-row-to-tree/
from typing import Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
def more(root: Optional[TreeNode], val: int, depth: int, is_Left: bool) -> Optional[TreeNode]:
if depth == 1:
if is_Left:
root = TreeNode(val, left=root, right=None)
else:
root = TreeNode(val, left=None, right=root)
return root
if root is None:
return root
root.left = more(root.left, val, depth - 1, True)
root.right = more(root.right, val, depth - 1, False)
return root
return more(root, val, depth, True)
if __name__ == "__main__":
so = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38